package org.liuyunfeng.web;

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.liuyunfeng.config.MultiConfiguration;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description:
 * @author: TanJingyu
 * @create: 2021-12-28 15:30
 **/
@RestController
@Slf4j
@RequestMapping("/hello")
public class HelloController implements DisposableBean {
    @Value("${msg}")
    private String msg;
    @Autowired
    private ApplicationContext context;
    @Autowired
    private MultiConfiguration.Multi multi;
    @Autowired
    private MultiConfiguration.Multi multiProd;
    @Autowired
    private BeanFactory beanFactory;


    @GetMapping("/multi/{operation:a|b}/{status:c|d}")
    public String multi() {
        return "到了";
    }

    @GetMapping("/response")
    @CrossOrigin(origins = "*")
    public void response(HttpServletResponse response) throws IOException {
        response.sendRedirect("http://www.baidu.com");
    }


    @RequestMapping("/upload")
    public void upload(MultipartFile file) {
        System.out.println(file.getOriginalFilename());
    }

    @RequestMapping
    public Object hello(HttpServletRequest request, String[] extendUrlParams) throws InterruptedException {

//        LaunchedURLClassLoader launchedURLClassLoader = (LaunchedURLClassLoader)this.getClass().getClassLoader();
//        for (URL url : launchedURLClassLoader.getURLs()) {
//            System.out.println(url.getPath());
//            System.out.println(url.getFile());
//        }

//        System.out.println(this.getClass().getClassLoader().getClass().getName());

//        log.info("来了一个请求");
//        TimeUnit.SECONDS.sleep(80);

//        HashMap<Object, Object> extendUrlParamsMap = new HashMap<>();
//        if (Objects.nonNull(extendUrlParams)) {
//            for (String extendUrlParam : extendUrlParams) {
//                String[] split = extendUrlParam.split("=");
//                extendUrlParamsMap.put(split[0], split[1]);
//            }
//        }

//        String username = MDC.get("username");
//        System.out.println(Thread.currentThread().getName() + ":   " + username);
//        MDC.put("username", "谭景玉");
//        MDC.clear();
//
//        return new HelloRequest().setAge(20);

        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.clear();

        Map<String, String[]> parameterMap1 = request.getParameterMap();


        return "success...";

    }

    @Override
    public void destroy() throws Exception {
        System.out.println("jvm shutdown......");
    }

    @Data
    @Accessors(chain = true)
    public static class HelloRequest {
        private Integer age;
    }

    public static void main(String[] args) {
        Object threeLock = new Object();
        Thread one = new Thread(() -> {
            System.out.println("one");
            synchronized (threeLock) {
                try {
                    threeLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread two = new Thread(() -> {
            synchronized (one) {
                try {
                    one.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("two");
            }
        });

        Thread three = new Thread(() -> {
            synchronized (two) {
                try {
                    two.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("three");
            }
        });




//        System.out.println(new Random().nextInt(10));
//        String orderNo = "en" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + new Random().nextInt(10) + 200000;
//        System.out.println(orderNo);
//        System.out.println(orderNo.length());
//
//        System.out.println("<script>" +
//                " SELECT IF(yi.id IS NULL,1,0) AS `status` ,t.*,yi.create_time as applyDate,yi.log_id as logId,IF(yi.c_status='',20,c_status) as cStatus,yi.is_invoice as isInvoice FROM(  SELECT  member_id AS memberId, order_id AS orderId, transactionid AS transactionId,money AS totalFee,date,pay_channel AS payChannel FROM yfb_information_member_record WHERE member_id = #{memberId} AND `status` = 1 AND money > 0 AND date > #{startTime} " +
//                " UNION ALL  " +
//                " SELECT member_id AS memberId, tradenum AS orderId, transactionid AS transactionId,totalfee AS totalFee,FROM_UNIXTIME(intime,'%Y-%m-%d %H:%i:%S') AS date,pay_channel  AS payChannel FROM yfb_paylog WHERE member_id = #{memberId} AND `stats` = 1 AND totalfee > 0 AND FROM_UNIXTIME(intime,'%Y-%m-%d %H:%i:%S') > #{startTime} " +
//                " UNION ALL  " +
//                " SELECT member_id AS memberId, tradenum AS orderId, transactionid AS transactionId,totalfee AS totalFee,create_time AS date,pay_channel  AS payChannel FROM yfb_attachment_paylog WHERE member_id = #{memberId} AND pay_status = 1 AND totalfee > 0 AND tradenum LIKE \"att%\"  AND create_time > #{startTime} ) t " +
//                " LEFT JOIN yfb_invoice yi ON t.memberId = yi.member_id AND t.orderId = yi.order_id  " +
//                " LEFT JOIN yfb_refund yr ON t.memberId = yr.member_id AND t.transactionId = yr.transaction_id " +
//                "<where>" +
//                "<if test=\" type != null and type != '' and type == 3 \">" +
//                "yi.id IS NOT NULL OR ( yi.id IS NULL AND (yr.refund_status != 1 OR yr.refund_status IS NULL)) " +
//                "</if>" +
//                "<if test=\" type != null and type != '' and type == 2 \">" +
//                "yi.id IS NULL AND (yr.refund_status != 1 OR yr.refund_status IS NULL) " +
//                "</if>" +
//                "<if test=\" type != null and type != '' and type == 1 and (payLogId == null or payLogId == '') \">" +
//                "yi.id IS NOT NULL OR (t.orderId LIKE 'dgzz%') OR (t.orderId LIKE 'attdgzz%') " +
//                "</if>" +
//                "<if test=\" type != null and type != '' and type == 1 and payLogId != null and payLogId != '' \">" +
//                "(yi.id IS NOT NULL and yi.log_id = #{payLogId}) OR (t.orderId LIKE 'dgzz%') OR (t.orderId LIKE 'attdgzz%') " +
//                "</if>" +
//                "</where>" +
//                "<choose>" +
//                "<when test=\" type != null and type != '' and type == 1  and (payLogId == null or payLogId == '') \">" +
//                " ORDER BY yi.update_time DESC" +
//                " </when>" +
//                "<otherwise>" +
//                " ORDER BY `status` DESC,date DESC LIMIT #{pageNo},#{pageSize}" +
//                " </otherwise>" +
//                " </choose>" +
//                "</script>");

//        Integer i = null;
//        System.out.println( == 5);

//        String str = "abcd.efg.pdf//";
//
//        System.out.println(str.replaceAll("/$", ""));

//        StringBuilder builder = new StringBuilder(str);
////        builder.insert(builder.lastIndexOf("k"), "前边");
//
//        System.out.println(builder.lastIndexOf("k"));
//
//        builder.replace(builder.indexOf("/"), builder.indexOf("/") + 1, "");
//
//        System.out.println(builder);


//        Object flag = null;
//
//        boolean newFlag = (boolean)flag;
//
//        System.out.println(newFlag);


        // 100
        int a = 4;

        System.out.println(3 % 4);

        // 4    100
        // 3    011
        System.out.println((a - 1) & 3);


    }
}

    